#!/usr/bin/env python
# -*- coding: utf-8 -*-

#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from lib.load_data import *
from zhineng_conn import *
from create_table import *
from config_tools import *
import urllib
import sys
import re
import time
reload(sys)
sys.setdefaultencoding('utf-8')


def load_order_info( date ,conn ,num):
    stat_conn = set_stats_conn()
    stat_table = "ad_order_info"
    path = "/work3/opt/analytic/lh_tmp_data/zhineng/order/"
    file_name = "zn_order_info_%s_%s.txt"%(date.replace('-',""),num)
    print "begin load order info table, file path : %s%s ." %(path,file_name) 
    load_data(stat_conn,path+file_name,stat_table,'\001')
    print "end load order info table."   
    conn.close()
    stat_conn.close()


def load_click_info(date):
    stat_conn = set_stats_conn()    
    date_str = date.replace('-',"")
    stat_table = "click_consumption"
    tomorrow = var_date(date).replace('-',"")    
    #print tomorrow
    
    path = "/work3/opt/analytic/lh_tmp_data/zhineng/click_data/"
    file_name = "click_data_%s.txt"%date_str
    
    q = "delete from %s where stat_date='%s'"%(stat_table,date)
    stat_conn.execute_sql(q)
    print "begin load click info table, file path : %s%s ." %(path,file_name) 
    load_data(stat_conn,path+file_name,stat_table,'$$')
    print "end load click info table." 
    
    stat_conn.close()

# refund info
def load_refund_info(date):
    stat_conn = set_stats_conn()    
    date_str = date.replace('-',"")
    stat_table = "ad_refund_info"
    tomorrow = var_date(date).replace('-',"")    

    path = "/work3/opt/analytic/lh_tmp_data/zhineng/refund_data/"
    file_name = "refund_data_%s.txt"%date_str
    print "begin load refund info table, file path : %s%s ." %(path,file_name) 
    load_data(stat_conn,path+file_name,stat_table,'\001')
    print "end load refund info table."
    
    stat_conn.close()
    

# display info
def load_display_info(date,sep):
    ib_conn = set_ib_conn()
    date_str = date.replace('-',"")
    month = get_date_info(date,'%Y%m')
    stat_table = "ad_display_info%s"%month
    try:
        create_display_info(ib_conn,stat_table)
    except Exception,e:
        print 'get_display_info ib create ad_display_info table error:',e
    ib_conn.close()
    print "load display info file. 4 ."
    for i in range(4):
        path = "/work3/opt/analytic/lh_tmp_data/zhineng/display_data/"
        file_name = "display_data_%s_%s.txt"%(date_str,i)
        
        try:
           ib_conn  = set_ib_conn()
           print "begin load display info %s file . file path : %s%s ." %(i, path, file_name)
           load_data(ib_conn,path+file_name,stat_table,sep)
           print "end load display info %s ." % i
        except Exception,e:
           print "ib ad_display_info load error:",e

        ib_conn.close()

# display info
def load_display_info_for_day(date,sep):
    ib_conn = set_ib_conn()   
    stat_conn = set_stats_conn()
    date_str = date.replace('-',"")
    month = get_date_info(date,'%Y%m%d')
    stat_table = "ad_display_info%s"%month
    try:
        create_display_info(ib_conn,stat_table)
    except Exception,e:
        print 'ib create display info table error:',e
    try:
	    create_display_info(stat_conn,stat_table)
    except Exception,e:
	print 'stat db create display info table error:',e
        
    path = "/work3/opt/analytic/lh_tmp_data/zhineng/display_data/"
    
    for i in range(4):
        print "begin load display data."
        file_name = "display_data_%s_%s.txt"%(date_str,i)
        print "begin load display info for day . file no.%s , file path : %s%s ." %(i, path, file_name)
        load_data(ib_conn,path+file_name,stat_table,sep)
        print "end load display info for day ."
	load_data(stat_conn,path+file_name,stat_table,sep)

    ib_conn = set_ib_conn()

def load_click_income_stats(date,sep):
    print 'start get_click_income_stats'
    stat_conn = set_stats_conn()
    date_str = date.replace('-',"")
    month = get_date_info(date,'%Y%m')
    stat_table = "ad_click_income_stats_%s"%month

    path = "/work3/opt/analytic/lh_tmp_data/zhineng/income/"
    file_name = "click_income%s.txt" % date_str
    q="delete from %s where stat_date='%s'"% (stat_table, date)
    stat_conn.execute_sql(q)
    ## create table
    print "begin load click income stats . file path : %s%s ." %(path, file_name)
    load_data(stat_conn,path+file_name,stat_table,sep)
    stat_conn.close()

    
def format_timestamp(ts_s):
    try:
        ts = int(ts_s)/1000
        return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(ts))
    except Exception:
        return get_yesterday()
   

def execute(date):
    print "income"
    try:
        pass
        #get_refund_info(date)
    except Exception,e:
        print 'get_refund_info:',e
    
    #time.sleep(600)
    try:
        pass
        load_click_income_stats(date)
    except Exception,e:
        print 'get_click_income_stats:',e
    
def main():
   inp = 0
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        inp = sys.argv[1]
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
    
   if inp == "1":
        yesterday = get_yesterday()
        get_all_order_info(yesterday)
   elif inp == "2":
        yesterday = get_yesterday()
        print yesterday
        get_disp(yesterday)
   else:
        stat_date=start_date   
        #conn = set_stats_conn()
        while stat_date>=start_date and stat_date<=end_date:
             print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
             #execute( stat_date)
             #get_refund_info(stat_date)
             #get_click_info(stat_date)
             #print "test"
	         load_display_info(stat_date,sep="\001")
             load_display_info_for_day(stat_date,sep="\001")
             #time.sleep(300)
             load_click_income_stats(stat_date,sep="\001")
             #date_str = stat_date.replace('-',"")    
             
             #month = get_date_info(stat_date,'%Y%m')
             #stat_table = "ad_display_info%s"%month
             #try:
                 #pass
                 #create_display_info(ib_conn,stat_table)
             #except Exception,e:
                 #pass
                 #print 'create table error:',e
                 
             #path = "/work3/opt/analytic/lh_tmp_data/zhineng/income/"
             #path = "/work3/opt/analytic/lh_tmp_data/zhineng/click_data/"
             #file_name = "click_data_%s.txt"%stat_date.replace('-','')
        
             #load_data(conn,path+file_name,'click_consumption_new','\t')
             #file_name = "click_income%s.txt"%stat_date.replace('-','')
             #
             #load_data(conn,path+file_name,'ad_click_income_stats','\t')
             
             print "The program of the date of %s is end at %s " %(stat_date,get_now())
             stat_date=var_date(stat_date)

### change load sep \t to \001 on 20130401
### income log change date on 20131231
### order log change data on 20131231
### click log $$

if __name__=='__main__':
    main()
